package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.ProductCovertHistoryDao;
import com.wanlian.entity.Card;
import com.wanlian.entity.ProductCovertHistory;
import com.wanlian.entity.User;
import com.wanlian.model.ResultModel;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProductConvertHistoryDaoImpl extends GenericDaoImpl<ProductCovertHistory> implements ProductCovertHistoryDao {

    public ProductConvertHistoryDaoImpl() {
        super(ProductCovertHistory.class);
    }

    /**
     * @param card
     * @param user
     * @return
     */
    @Override
    public ResultModel addByCardandUser(Card card, User user) {
        return null;
    }

    /**
     * 通过用户查询 商品兑换记录
     *
     * @param page
     * @param userId
     * @return
     */
    @Override
    public ResultModel queryHistoryByUser(Page page, int userId) {
        List<ProductCovertHistory> lists = new ArrayList<ProductCovertHistory>();
        String hql = String.format("from %s where userId=?", entityClassName());
        lists = this.queryForListByPage(hql, new Object[]{userId}, page);
        String coungHql = String.format("select count(*) from %s where userId=? ", entityClassName());
        Long count = this.queryCount(coungHql, new Object[]{userId});
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }

    /**
     * 查询 商品兑换记录
     *
     * @param pageCondition
     * @return
     */
    @Override
    public ResultModel queryHistoryByConditions(PageCondition pageCondition) {
        List<ProductCovertHistory> lists = new ArrayList<ProductCovertHistory>();
        String hql = String.format("from %s", entityClassName());
        lists = this.queryForListByPage(hql, null, pageCondition.getPage());
        String coungHql = String.format("select count(*) from %s where userId=? ", entityClassName());
        Long count = this.queryCount(coungHql,null);
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }
}
